package cn.yxsm.yxmall.controller;

import cn.yxsm.yxmall.mapper.ProductMapper;
import cn.yxsm.yxmall.pojo.dto.ProductDTO;
import cn.yxsm.yxmall.pojo.dto.ProductSelectDTO;
import cn.yxsm.yxmall.pojo.dto.ProductUpdateDTO;
import cn.yxsm.yxmall.pojo.entity.Product;
import cn.yxsm.yxmall.pojo.vo.ProductAdminVO;
import cn.yxsm.yxmall.pojo.vo.ProductDetailVO;
import cn.yxsm.yxmall.pojo.vo.ProductUpdateVO;
import cn.yxsm.yxmall.pojo.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Value("${dirPath}")
    private String dirPath;

    @Autowired(required = false)
    ProductMapper mapper;
    @RequestMapping("/select/admin")
    public List<ProductAdminVO> select(){
        return mapper.selectAdmin();
    }

    @RequestMapping("/insert")
    public List<ProductAdminVO> insert(@RequestBody ProductDTO product){
        System.out.println("product = " + product);
        Product p = new Product();
        BeanUtils.copyProperties(product,p);
        p.setCreated(new Date());
        p.setViewCount(0);
        p.setSaleCount(0);
        mapper.insert(p);
        return mapper.selectAdmin();
    }

    @RequestMapping("/delete")
    public List<ProductAdminVO> delete(int id){
        String url = mapper.selectUrlById(id);
        String content = mapper.selectUrlById(id);
        new File(dirPath+url).delete();
        new File(dirPath+content).delete();
        mapper.deleteById(id);
        return mapper.selectAdmin();
    }
    @RequestMapping("/selectById/update")
    public ProductUpdateVO selectByIdUpdate(int id){
        return mapper.selectByIdUpdate(id);
    }
    @RequestMapping("/update")
    public void update(@RequestBody ProductUpdateDTO product){
        mapper.update(product);
    }
    @RequestMapping("/deleteSelectAllProducts")
    public List<ProductAdminVO> deleteSelectAllProducts(@RequestBody ArrayList<Integer> ids){
        mapper.deleteSelectAllProducts(ids);
        return mapper.selectAdmin();
    }
    @RequestMapping("/selectSomeProducts")
    public List<ProductAdminVO> selectSomeProducts(@RequestBody ProductSelectDTO product) {

        if (product.getTitle() != null && product.getBrandId() != null && product.getCategoryId() != null) {
            return mapper.selectSomeProducts(product);
        } else if (product.getTitle() != null && product.getBrandId() != null && product.getCategoryId() == null) {
            return mapper.selectSomeProductsTAB(product);
        } else if (product.getTitle() != null && product.getBrandId() == null && product.getCategoryId() != null) {
            return mapper.selectSomeProductsTAC(product);
        } else if (product.getTitle() != null && product.getBrandId() == null && product.getCategoryId() == null) {
            return mapper.selectSomeProductsT(product);
        } else if (product.getTitle() == null && product.getBrandId() != null && product.getCategoryId() != null) {
            return mapper.selectSomeProductsBAC(product);
        } else if (product.getTitle() == null && product.getBrandId() != null && product.getCategoryId() == null) {
            return mapper.selectSomeProductsB(product);
        }
        return mapper.selectSomeProductsC(product);
    }
    @RequestMapping("/select")
    public List<ProductVO> selectAllProduct(){
        return mapper.selectAllProduct();
    }
    @RequestMapping("/selectById/detail")
    public ProductDetailVO selectByIdDetail(int id, HttpSession session){
        System.out.println("id = " + id);
        //从会话对象中获取商品id的相关信息，第一次回去不到
        String info = (String) session.getAttribute("view"+id);
        if(info==null){
            mapper.updateViewCount(id);//浏览量加1
            //把当前商品id相关信息存储到session中，再次访问此商品，此条件不满足，值不为空
            session.setAttribute("view"+id,"isVisited");
        }
        return mapper.selectByIdDetail(id);
    }
}
